﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Administrator
{
    public partial class ucJoinChannel : UserControl
    {
        private Int32[] MetersIdx = new Int32[0];
        private Boolean LoadSetting = false;

        public Int32 ID = -1;
        public Boolean CollectEnable = false;
        public Byte EnergyType = 0;
        public String MeterSerial = "";
        public Int32 MeterID = -1;
        public String ExportNPATSCode = "";
        public String ImportNPATSCode = "";
        public String Value63002Code = "";
        public String ASKPIdentifier = "";
        
        public ucJoinChannel()
        {
            InitializeComponent();
        }

        //Заполнение полей структуры и формы
        public void FillFields(Int32 ChannelID, Boolean ChannelCollectEnable, Byte ChannelEnergyType, String ChannelMeterSerial, Int32 ChannelMeterID, String ChannelExportNPATSCode, String ChannelImportNPATSCode, String ChannelValue63002Code, String ChannelASKPIdentifier)
        {
            Int32 i, MeterIdx;

            ID = ChannelID;
            CollectEnable = ChannelCollectEnable;
            EnergyType = ChannelEnergyType;
            MeterSerial = ChannelMeterSerial;
            MeterID = ChannelMeterID;
            ExportNPATSCode = ChannelExportNPATSCode;
            ImportNPATSCode = ChannelImportNPATSCode;
            Value63002Code = ChannelValue63002Code;
            ASKPIdentifier = ChannelASKPIdentifier;

            LoadSetting = true;
            chbChannelCollectState.Checked = CollectEnable;
            tbChannelNumber.Text = (ID + 1).ToString();
            cbEnergyType.SelectedIndex = EnergyType;
            tbMeterSerial.Text = MeterSerial;
            
            cbMeterType.SelectedIndex = -1;
            cbMeterType.Items.Clear();            
            MetersIdx = new Int32[0];
            for (i = 0; i < cMain.MeterList.Length; i++)
            {
                if (!cMain.MeterList[i].ItemAction.Deleted)
                {
                    Array.Resize(ref MetersIdx, MetersIdx.Length + 1);
                    cbMeterType.Items.Add(cMain.MeterList[i].Caption + " (" + cMain.MeterList[i].Precision.ToString("F1") + "/" + cMain.MeterList[i].BeforeComma.ToString() + "." + cMain.MeterList[i].AfterComma.ToString() + "/" + cMain.MeterList[i].MeterConst.ToString() + ")");
                    MetersIdx[MetersIdx.Length - 1] = i;
                    if (MeterID == cMain.MeterList[i].ID)
                        cbMeterType.SelectedIndex = cbMeterType.Items.Count - 1;
                }
            }
            MeterIdx = cMain.GetMeterIndexByID(MeterID);            
            if ((MeterIdx >= 0) && (MeterIdx < cMain.MeterList.Length))
            {                
                tbMeterPrecision.Text = cMain.MeterList[MeterIdx].Precision.ToString();
                tbMeterCapacity.Text = cMain.MeterList[MeterIdx].BeforeComma.ToString() + "." + cMain.MeterList[MeterIdx].AfterComma.ToString();
                tbMeterConst.Text = cMain.MeterList[MeterIdx].MeterConst.ToString();           
            }
            else 
            {                
                tbMeterPrecision.Text = "";
                tbMeterCapacity.Text = "";
                tbMeterConst.Text = "";
            }            
            tbExportNPATSCode.Text = ExportNPATSCode;
            tbImportNPATSCode.Text = ImportNPATSCode;
            tbValue63002Code.Text = Value63002Code;
            tbASKPIdentifier.Text = ASKPIdentifier;
            LoadSetting = false;
        }

        private void chbChannelCollectState_CheckedChanged(object sender, EventArgs e)
        {
            if (!LoadSetting) CollectEnable = chbChannelCollectState.Checked;            
        }
        private void cbEnergyType_SelectedIndexChanged(object sender, EventArgs e)
        {
            Int32 Idx = -1;

            if (!LoadSetting)
            {
                if (sender == cbEnergyType) EnergyType = (Byte)cbEnergyType.SelectedIndex;
                if (sender == cbMeterType)
                {
                    Idx = cbMeterType.SelectedIndex;
                    if ((Idx >= 0) && (Idx < MetersIdx.Length))
                    {
                        MeterID = cMain.MeterList[MetersIdx[Idx]].ID;
                        tbMeterPrecision.Text = cMain.MeterList[MetersIdx[Idx]].Precision.ToString();
                        tbMeterCapacity.Text = cMain.MeterList[MetersIdx[Idx]].BeforeComma.ToString() + "." + cMain.MeterList[MetersIdx[Idx]].AfterComma.ToString();
                        tbMeterConst.Text = cMain.MeterList[MetersIdx[Idx]].MeterConst.ToString();
                    }
                    else
                    {
                        MeterID = -1;
                        tbMeterPrecision.Text = "";
                        tbMeterCapacity.Text = "";
                        tbMeterConst.Text = "";
                    }            
                }
            }
        }
        private void tbMeterSerial_TextChanged(object sender, EventArgs e)
        {
            
            if (!LoadSetting)
            {
                if (sender == tbMeterSerial)
                {
                    if (Checker.IsLatinString(((TextBox)sender).Text))
                        MeterSerial = ((TextBox)sender).Text;   
                    else
                        ((TextBox)sender).Text = MeterSerial;
                }

                if (sender == tbExportNPATSCode)
                {
                    if (Checker.IsLatinString(((TextBox)sender).Text))
                        ExportNPATSCode = ((TextBox)sender).Text;
                    else
                        ((TextBox)sender).Text = ExportNPATSCode;
                }
                    
                if (sender == tbImportNPATSCode)
                {
                    if (Checker.IsLatinString(((TextBox)sender).Text))
                        ImportNPATSCode = ((TextBox)sender).Text;
                    else
                        ((TextBox)sender).Text = ImportNPATSCode;
                }
                    
                if (sender == tbValue63002Code)
                {
                    if (Checker.IsLatinString(((TextBox)sender).Text))
                        Value63002Code = ((TextBox)sender).Text;
                    else
                        ((TextBox)sender).Text = Value63002Code;
                }

                if (sender == tbASKPIdentifier)
                {
                    if (Checker.IsLatinString(((TextBox)sender).Text))
                        ASKPIdentifier = ((TextBox)sender).Text;
                    else
                        ((TextBox)sender).Text = ASKPIdentifier;
                }

                Application.DoEvents();
            }
        }       
    }
}
